package com.chiancloud.shield.core.registry.info;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wanghui on 14-8-20.
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServiceInstanceData implements Serializable {
    public String serviceType;
    public String id;
    public String description;
    public String yarnApplicationId;
    public long registrationTimeUTC;

    public RegistryView internalView = new RegistryView();

    public RegistryView externalView = new RegistryView();

    public ServiceInstanceData() {
    }

    public ServiceInstanceData(String id, String serviceType) {
        this.serviceType = serviceType;
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        ServiceInstanceData that = (ServiceInstanceData) o;

        if (!id.equals(that.id)) {
            return false;
        }
        if (!serviceType.equals(that.serviceType)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = serviceType.hashCode();
        result = 31 * result + id.hashCode();
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb =
                new StringBuilder("ServiceInstanceData{");
        sb.append("id='").append(id).append('\'');
        sb.append(", serviceType='").append(serviceType).append('\'');
        sb.append('}');
        return sb.toString();
    }

    public RegistryView getRegistryView(boolean external) {
        return external ? externalView : internalView;
    }

    public Map<String, RegisteredEndpoint> listEndpoints(boolean external) {
        RegistryView view = getRegistryView(external);
        if (view == null) {
            return new HashMap<String, RegisteredEndpoint>(0);
        }
        Map<String, RegisteredEndpoint> endpoints = view.endpoints;
        if (endpoints != null) {
            return endpoints;
        } else {
            return new HashMap<String, RegisteredEndpoint>(0);
        }
    }

}
